/**
 * Created by ROGSTAR on 2016/11/7.
 */
require.config(window.jshead);
require(['jquery','layer','include/navigator'], function($,layer) {
    layer.config({
        path: '/js/layer/'
    });
    //图片验证码
    $('.validImg').click(function() {
        $(".validImg").prop("src", "/sys/ImageServlet?time=" + new Date());
    });
    $('#passwordNoSee').bind('click',function(){
        if($(this).data().type==1){
            $('#password').prop('type','text');
            $("#passwordNoSee").attr("src","/img/mimakejian.png");
            $(this).data().type=2;
        }else if($(this).data().type==2){
            $('#password').prop('type','password');
            $("#passwordNoSee").attr("src","/img/mimabukejian.png");
            $(this).data().type=1;
        }
        // $('#passwordSee').removeClass('display')
        // $('#passwordNoSee').addClass('display')
    });
    // $('#passwordSee').bind('click',function(){
    //     $('#password').prop('type','password');
    //     $('#passwordNoSee').removeClass('display')
    //     $('#passwordSee').addClass('display')
    // });


    function Trim(str,is_global)
    {
        var result;
        result = str.replace(/(^\s+)|(\s+$)/g,"");
        if(is_global.toLowerCase()=="g")
        {
            result = result.replace(/\s/g,"");
        }
        return result;
    }

    var href=Trim(decodeURI(window.location.href),'g');
    var invite='';
    var inviteString='';
    if(href.indexOf('invite')!=-1){
        inviteString=href.split('invite=')[1];
        if(inviteString.indexOf('&')!=-1){
            invite=inviteString.split('&')[0];
        }else{
            invite=inviteString;
        }
    }

    var source='';
    if(href.indexOf('source')!=-1){
        source=href.split('source=')[1];
    }




    $('#clickMes').bind('click',function(){
        var phone=$('#phone').val();
        var verification=$('#verification').val();
        var password=$('#password').val();
        var validNum=$("#code").val();
        if(phone==""){
            layer.tips("请先输入手机号",'#phone');
            return;
        }
        if (!/^[1][3,4,5,7,8][0-9]{9}$/.exec(phone)) {
            layer.tips("请输入正确的手机号",'#phone');
            return;
        }
        if(validNum==""){
            layer.tips("请先输入验证码",'#code');
            return;
        }
        var that=$(this);
        time(that);

        //短信接口
        $.ajax({
            type: "POST",
            url: "/registerSendSms",
            data: {
                mobile:phone,
                pictureCode: validNum
            },
            dataType: "json",
            success: function (data) {
                if(data.result=='success'){
                    layer.msg("短信已发送！");
                }else{
                    wait=0;
                    layer.msg(data.messageText);
                }
            }
        });
        //短信接口end
    });

    var wait=60;
    function time(o) {
        if (wait == 0) {
            o.removeAttr("disabled");
            o.val('获取验证码');
            o.css("color", '#ffffff');
            o.css("background","#2c97f4");
            wait = 60;
            $('.validImg').trigger('click')
        } else {
            o.attr("disabled", true);
            o.css("color", '#fff');
            o.css("background", '#ddd');
            o.val("重新发送(" + wait + "s)");
            wait--;
            setTimeout(function() {
                    time(o)
                },
                1000)
        }
    }

    $('#register').bind('click',function(){
        var phone=$('#phone').val();
        var verification=$('#verification').val();
        var password=$('#password').val();
        var validNum=$("#code").val();
        var peopleChoice = $('#people-choice').val();

        var checkbox=document.getElementById('checkbox').checked;

        if(phone==""){
            layer.tips("请先输入11位手机号",'#phone');
            return;
        }
        if (!/^[1][3,4,5,7,8][0-9]{9}$/.exec(phone)) {
            layer.tips("请输入正确的手机号",'#phone');
            return;
        }
        if(verification==""){
            layer.tips("请先输入手机验证码",'#verification');
            return;
        }
        if(password==""){
            layer.tips("请先设置密码",'#password');
            return;
        }
        if(password.length<6){
            layer.tips("密码不能小于6位",'#password');
            return;
        }
        if(checkbox==false){
            layer.tips("请先勾选服务条款协议",'#checkbox');
            return;
        }
        if(validNum==""){
            layer.tips("请输入正确的验证码",'#code');
            return;
        }
        //图片验证
        //图片验证码
        $('.validImg').click(function() {
            $(".validImg").attr("src", "/sys/ImageServlet?time=" + new Date());
        });
        var index = layer.load(1, {
            shade: [0.1,'#fff'] //0.1透明度的白色背景
        });
        $.ajax({
            type: "POST",
            url: "/register/pcRegister",
            data: {
                mobile:phone,
                mobileCode:verification,
                password:password,
                pictureCode: validNum,
                source:source,
                invite:peopleChoice
            },
            dataType: "json",
            success: function (data) {
                layer.close(index)
                if(data.result=='success'){
                    layer.msg("注册成功");
                    setTimeout(function () {
                        window.location.href="/login";
                    },1000)
                }else{
                    layer.alert(data.messageText);
                }



            }
        });

    });
    //用户名输入后可点击删除
    $("#phone").bind("input propertychange",function(){
        var phone =$('#phone').val();
        var userDel = $(".login_input_wrap .user_del");
        if(phone){
            userDel.css({"display":"block"});
            userDel.bind("click", function (){
                $('#phone').val("");
                userDel.css({"display":"none"});
            })
        }else{
            userDel.css({"display":"none"});
        }
    })

    $(".login-content").keydown(function() {
        if (event.keyCode == "13") {//keyCode=13是回车键
            $('#register').click();
        }
    });

    /*联系人选填交互效果*/
    $('.login-choice').bind('click',function(){
        if($('.login-choicephopne').is(':hidden')){
            $('.login-choicephopne').show()
            $('.icon-left').css('transform','rotate(90deg)');
        }else{
            $('.login-choicephopne').hide();
            $('.icon-left').css('transform','rotate(0deg)');
        }
    })

    //校验邀请码

    $('#people-choice').bind('blur',function(){
        var reg = /^[0-9a-zA-Z]*$/g;
        var val= $('#people-choice').val()
        if(!reg.test(val)){
            layer.tips('请输入正确的邀请码','#people-choice')
            $('#people-choice').val('')
        }
    })

});